package cc.mrbird.statistical.service.impl;

import cc.mrbird.assets.dao.AssetManaMapper;
import cc.mrbird.assets.domain.AssetType;
import cc.mrbird.statistical.dao.UnitAnalysisMapper;
import cc.mrbird.statistical.domain.UnitAnalysis;
import cc.mrbird.statistical.service.UnitAnalysisService;
import cc.mrbird.common.service.impl.BaseService;
import cc.mrbird.system.domain.User;
import cc.mrbird.unit.dao.UnitInfoMapper;
import cc.mrbird.unit.dao.UserUnitMapper;
import cc.mrbird.unit.domain.UnitInfo;
import cc.mrbird.unit.domain.UserUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("unitAnalysisService")
public class UnitAnalysisServiceImpl extends BaseService<UnitAnalysis> implements UnitAnalysisService {

    @Autowired
    private AssetManaMapper assetManaMapper;
    @Autowired
    private UserUnitMapper userUnitMapper;
    @Autowired
    private UnitInfoMapper unitInfoMapper;
    @Autowired
    private UnitAnalysisMapper unitAnalysisMapper;
    //故障率统计 = 所有未正常的设备/所有设备
    @Override
    public Map<String, Float> failureRateStatistics(String nextTime, User currentUser) {
        Map<String,Float> failureRateStatisticMap = new HashMap<>();
        Long userId = currentUser.getUserId();
        UserUnit unitIdByUser = userUnitMapper.findUnitIdByUser(currentUser.getUserId());
        UnitInfo unitInfo = unitInfoMapper.findByUnitId(unitIdByUser.getUnit_id());
        List<AssetType> totalType = unitAnalysisMapper.findTotalType(unitInfo.getUnit_id());
        float failureRate = 0;
        if(!totalType.isEmpty()){
            for (int i = 0; i < totalType.size(); i++) {
                String asset_type_name = totalType.get(i).getAsset_type_name();
                Integer failureRateByType = unitAnalysisMapper.findFailureRateByType(asset_type_name, nextTime,unitInfo.getUnit_id());
                Integer AllByType = unitAnalysisMapper.findAllByType(asset_type_name, nextTime,unitInfo.getUnit_id());
                failureRate = (float)failureRateByType / AllByType;
                failureRateStatisticMap.put(asset_type_name,failureRate);
            }
            return failureRateStatisticMap;
        }
        return null;
    }
}
